import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
// 引入自动引入插件
import Components from "unplugin-vue-components/vite";
// 自动引入element等组件
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import AutoImport from "unplugin-auto-import/vite";

import path from "path";
// https://vitejs.dev/config/

// 引入svg图标组件
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
// 引入mock
import { viteMockServe } from "vite-plugin-mock";

export default defineConfig(({ command, mode }) => {
  //获取各种环境下的对应变量
  let env = loadEnv(mode, process.cwd());
  return {
    plugins: [
      vue(),
      AutoImport({
        imports: ["vue", "pinia", "vue-router"],
      }),
      Components({
        // 指定解决element自动组件导入
        resolvers: [ElementPlusResolver()],
      }),
      createSvgIconsPlugin({
        // Specify the icon folder to be cached  图标存放路径
        iconDirs: [path.resolve(process.cwd(), "src/assets/icons")], //
        // Specify symbolId format
        symbolId: "icon-[dir]-[name]",
      }),
      // mock数据配置
      viteMockServe({
        enable: command === "serve",
      }),
    ],
    resolve: {
      alias: {
        "@": path.resolve("./src"), // 相对路径别名配置，使用 @ 代替 src
      },
    },

    css: {
      preprocessorOptions: {
        // 加载全局配置scss
        scss: {
          javascriptEnabled: true,
          additionalData: '@import "./src/styles/variable.scss";',
        },
      },
    },

    //代理跨域
    server: {
      proxy: {
        [env.VITE_APP_BASE_API]: {
          target: env.VITE_SERVE,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ""),
        },
      },
    },
  };
});
